/**
  * @file	WHGM5.c
  *	@author	yoyoyes
  * @date	2023-6-10
  */
  
#include "WHGM5.h"
#include "usart.h"
#include "systick.h"
#include "string.h"
#include "gd32f10x.h"

/**
  * @func	WHGM5_init
  * @param	none
  * @return	none
  */
void WHGM5_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOB);	  
	
	gpio_init(GPIOB, GPIO_MODE_IPD, GPIO_OSPEED_50MHZ, GPIO_PIN_0);        
	gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
}

/**
  * @func	WHGM5_init
  * @param	none
  * @return	none
  */
void WHGM5_reset(void)
{
	usart0_printf("\r\n");
	usart0_printf("[info] WHGM5 4G Cat-1 reset...\r\n");
	gpio_bit_set(GPIOB, GPIO_PIN_2);
	systick_delay_ms(500);
	gpio_bit_reset(GPIOB, GPIO_PIN_2);
}

/**
  * @func	WHGM5_config_mode_enter
  * @param	none
  * @return	none
  */
void WHGM5_config_mode_enter(void)
{
	uint16_t data_len;
	
	while(1)
	{
		data_len = usart_rx_buff_read(&usart2_rx_control_block, usart2_rx_package);
		if(data_len > 0)
		{
			if((data_len == 6) && (memcmp(usart2_rx_package, "WH-GM5", 6) == 0))
				usart2_printf("+++");
			
			if((data_len == 1) && (memcmp(usart2_rx_package, "a", 1) == 0))
				usart2_printf("a");
			
			if((data_len == 5) && (memcmp(usart2_rx_package, "+ok\r\n", 5) == 0))
			{
				usart0_printf("[info] 4G module enter config mode!\r\n");
				break;
			}
		}
	}
}

/**
  * @func	WHGM5_config_mode_exit
  * @param	none
  * @return	none
  */
void WHGM5_config_mode_exit(void)
{
	uint16_t data_len;
	
	usart2_printf("AT+ENTM\r\n");
	
	while(1)
	{
		data_len = usart_rx_buff_read(&usart2_rx_control_block, usart2_rx_package);
		if(data_len > 0)
		{
			if((data_len == 15) && (memcmp(usart2_rx_package, "AT+ENTM\r\n\r\nOK\r\n", 15) == 0))
			{
				usart0_printf("[info] 4G module exit config mode!\r\n");
				break;
			}
		}
	}	
}
